package com.zizo.autohome.user.crawl;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import com.zizo.file.util.FileUtil;

public class UserFileHttpClient implements Runnable {
	private final CloseableHttpClient httpClient;
	private final HttpGet httpget;
	private final int userid;

	public UserFileHttpClient(CloseableHttpClient httpClient, HttpGet httpget, int userid) {
		super();
		this.httpClient = httpClient;
		this.httpget = httpget;
		this.userid = userid;
	}

	@Override
	public void run() {
		try {
			// 执行请求
			CloseableHttpResponse response = httpClient.execute(httpget);
			// 获取实体
			HttpEntity ety = response.getEntity();
			// System.out.println(EntityUtils.toString(ety, "UTF-8"));
			String content = EntityUtils.toString(ety, "UTF-8");
			if (!content.contains("503 Service Temporarily Unavailable")) {
				FileUtil.writeFile(EntityUtils.toString(ety, "UTF-8"), "C:/Data/a/" + userid + ".txt", false);
				EntityUtils.consume(ety);// 释放实体
				response.close();// 关闭响应
				System.out.println(userid + " Finish");
			} else {
				System.out.println(userid + " Kill");
			}
		} catch (Exception e) {
		}
	}

	public static void main(String[] args) throws Exception {
		List<Integer> userids = UserUtil.getUserID();
		// 请求首部--可选的，User-Agent对于一些服务器必选，不加可能不会返回正确结果
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000)
				.setCookieSpec(CookieSpecs.DEFAULT).build();
		PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();// 连接的线程池
		// 定义HttpClient
		CloseableHttpClient httpClient = HttpClients.custom()
				.setKeepAliveStrategy(UserUtil.getConnectionKeepAliveStrategy())
				.setRetryHandler(UserUtil.getHttpRequestRetryHandler()).setConnectionManager(cm).build();
		// 将最大连接数增加到200
		cm.setMaxTotal(20);
		// 将每个路由基础的连接增加到20
		cm.setDefaultMaxPerRoute(20);
		// 将目标主机的最大连接数增加到50
		HttpHost localhost = new HttpHost("124.239.197.134", 80);
		cm.setMaxPerRoute(new HttpRoute(localhost), 20);
		ExecutorService fix = Executors.newFixedThreadPool(1);
		for (Integer userid : userids) {
			HttpGet get = new HttpGet(
					"http://i.autohome.com.cn/ajax/home/OtherHomeAppsData?appname=Car&r=0.6292840484228965&TuserId="
							+ userid);
			get.setHeader("User-Agent",
					"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
			get.setHeader("Accept", "*/*");
			get.setHeader("X-Requested-With", "XMLHttpRequest");
			get.setHeader("Accept-Encoding", "gzip, deflate, sdch");
			get.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
			get.setHeader("Referer", "124.239.197.134:80/" + userid);
			get.setConfig(requestConfig);
			UserFileHttpClient uf = new UserFileHttpClient(httpClient, get, userid);
			fix.execute(uf);
		}
		fix.shutdown();
	}
}
